package postfix;
import postfix.analysis.*;
import postfix.node.*;
class Translation extends DepthFirstAdapter
{
	private Start start;
	
	public void inStart(Start node)
	{
		start=node;
		System.out.println(start);
	}

	public void outAExprTerm(AExprTerm node)
	{
		int term = Integer.parseInt(((ANumberTerm)((ATermFactor)((AFactorExpr)node.getExpr()).getFactor()).getTerm()).getNumber().getText());
		node.parent().replaceBy(new ATermFactor(new ANumberTerm(new TNumber(String.valueOf(term)))));
	}
	
	public void outAPlusExpr(APlusExpr node)
	{// out of alternative {plus} in Expr, we print the plus.
		int expr = Integer.parseInt(((ANumberTerm)((ATermFactor)((AFactorExpr)node.getExpr()).getFactor()).getTerm()).getNumber().getText());
		int factor = Integer.parseInt(((ANumberTerm)((ATermFactor)node.getFactor()).getTerm()).getNumber().getText());
		
		expr = factor+expr;
		
		node.replaceBy(new AFactorExpr(new ATermFactor(new ANumberTerm(new TNumber(String.valueOf(expr))))));
		System.out.println(start);
	}
	public void outAMinusExpr(AMinusExpr node)
	{// out of alternative {minus} in Expr, we print the minus.
		int expr = Integer.parseInt(((ANumberTerm)((ATermFactor)((AFactorExpr)node.getExpr()).getFactor()).getTerm()).getNumber().getText());
		int factor = Integer.parseInt(((ANumberTerm)((ATermFactor)node.getFactor()).getTerm()).getNumber().getText());

		expr = factor-expr;
		
		node.replaceBy(new AFactorExpr(new ATermFactor(new ANumberTerm(new TNumber(String.valueOf(expr))))));
		System.out.println(start);
	}
	public void outAMultFactor(AMultFactor node)
	{// out of alternative {mult} in FactorSystem.out.println(node.getExpr()));, we print the mult.
		int term = Integer.parseInt(((ANumberTerm)node.getTerm()).getNumber().getText());
		int factor = Integer.parseInt(((ANumberTerm)((ATermFactor)node.getFactor()).getTerm()).getNumber().getText());

		term = factor*term;

		node.replaceBy(new ATermFactor(new ANumberTerm(new TNumber(String.valueOf(term)))));
		System.out.println(start);
	}
	public void outADivFactor(ADivFactor node)
	{// out of alternative {div} in Factor, we print the div.
		int term = Integer.parseInt(((ANumberTerm)node.getTerm()).getNumber().getText());
		int factor = Integer.parseInt(((ANumberTerm)((ATermFactor)node.getFactor()).getTerm()).getNumber().getText());
		
		term = factor/term;

		node.replaceBy(new ATermFactor(new ANumberTerm(new TNumber(String.valueOf(term)))));
		System.out.println(start);
	}
	public void outAModFactor(AModFactor node)
	{// out of alternative {mod} in Factor, we print the mod.
		int term = Integer.parseInt(((ANumberTerm)node.getTerm()).getNumber().getText());
		int factor = Integer.parseInt(((ANumberTerm)((ATermFactor)node.getFactor()).getTerm()).getNumber().getText());
		
		term = factor%term;

		node.replaceBy(new ATermFactor(new ANumberTerm(new TNumber(String.valueOf(term)))));
		System.out.println(start);
	}
}